package cn.mediinfo.dianzidp.controller;

import cn.com.listentech.LEDControl;
import cn.com.listentech.ScreenInfo;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.mediinfo.dianzidp.builder.JianYanLEDBuilder;
import cn.mediinfo.dianzidp.builder.PaiBanXXLEDBuilder;
import cn.mediinfo.dianzidp.builder.YaoFangLEDBuilder;
import cn.mediinfo.dianzidp.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping(value = {"/api/v1/DianZiDPJH", "/api/v1.0/DianZiDPJH"})
public class ShengZhouController {
    private final JianYanLEDBuilder jianYanLEDBuilder;
    private final YaoFangLEDBuilder yaoFangLEDBuilder;

    private final PaiBanXXLEDBuilder paiBanXXLEDBuilder;

    public ShengZhouController(JianYanLEDBuilder jianYanLEDBuilder, YaoFangLEDBuilder yaoFangLEDBuilder, PaiBanXXLEDBuilder paiBanXXLEDBuilder) {
        this.jianYanLEDBuilder = jianYanLEDBuilder;
        this.yaoFangLEDBuilder = yaoFangLEDBuilder;
        this.paiBanXXLEDBuilder = paiBanXXLEDBuilder;
    }

    /**
     * 测试接口
     * @return
     * @throws RuntimeException
     * @throws IOException
     * @throws UnsupportedEncodingException
     * @throws ParseException
     */
    @PostMapping("test")
    public Boolean test() throws RuntimeException, IOException, UnsupportedEncodingException, ParseException {
        String ls_head = "   1号窗口   2号窗口   3号窗口   4号窗口   5号窗口   6号窗口   7号窗口   8号窗口";
        int gi_handle = LEDControl.StartSend();
        log.error("LED startSend返回值:{}",gi_handle);
        String ls_ip= "192.168.17.156";
        int transMode = LEDControl.SetTransMode(gi_handle, 1, 0, 3, 1);
        log.error("LED transMode返回值:{}",transMode);
        int netWorkPara = LEDControl.SetNetworkPara(gi_handle, 1, ls_ip,10000);
        log.error("LED SetNetworkPara 返回值{}",netWorkPara);
        ScreenInfo sc = new ScreenInfo();
        sc.screenwidth = 1216;
        sc.screenheight = 160;
        sc.dbcolor = 1;
        sc.baud = 115200;
        sc.screenname = "";
        int screenInfo =LEDControl.SetScreenInfo(gi_handle, sc);
        log.error("LED SetScreenInfo 返回值{}",screenInfo);
        int addControl = LEDControl.AddControl(gi_handle, 1, 2);
        log.error("LED AddControl 返回值{}",addControl);
        int addProgram =  LEDControl.AddProgram(gi_handle, 1, 0);
        log.error("LED addProgram 返回值{}",addProgram);
        int addQuit = LEDControl.AddQuitText(gi_handle, 1, 1, 0, 5, 1200, 60, 255, "宋体", 30, true, false, false, ls_head);
        log.error("LED addQuit 返回值{}",addQuit);
        try {
            System.in.read();
        } catch (Exception var2) {

        }
        LEDControl.EndSend(gi_handle);
        return true;
    }

    @PostMapping("sendJinRiPaiBanXX")
    public Boolean sendJinRiPaiBanXX(List<JZ_GH_JinRiHYListDto> paiBanXX) throws RuntimeException, IOException, UnsupportedEncodingException {
        List<PaiBanKSXXDto> paiBanKSXXDtos = new ArrayList<>();
        if (CollectionUtil.isEmpty(paiBanXX)){
            return true;
        }
        for (JZ_GH_JinRiHYListDto banXX : paiBanXX) {
            paiBanKSXXDtos.add(zhuanHuaHYXX(banXX));
        }
        /**
         * 具体的发送代码
         */
        paiBanXXLEDBuilder.wf_send_dp1_fp(paiBanKSXXDtos);
        return true;
    }
    /**
     * 传递检验窗口信息
     * @param
     * @return
     * @throws RuntimeException
     * @throws IOException
     * @throws UnsupportedEncodingException
     * @throws ParseException
     */
    @PostMapping("sendJianYanChuangKouXX")
    public Boolean sendJianYanChuangKouXX(@RequestBody List<JianYanPinDto> chuangKouXXData) throws RuntimeException, IOException, UnsupportedEncodingException, ParseException {

        String jiGouId ="";
        //一般来说一个串口只会有一个窗口XXData
        Map<String, List<JianYanPinDto>> jianYanPing = chuangKouXXData.stream().filter(x -> StrUtil.isNotBlank(x.getChuangKouId()))
                .collect(Collectors.groupingBy(JianYanPinDto::getChuangKouId));
        Map<String, JianYanPinDto> jianYanPings = new HashMap<>();
        jianYanPing.forEach((k,v)->{
            if (v!=null && !v.isEmpty()){
                jianYanPings.put(k,v.get(0));
            }

        });
        return jianYanLEDBuilder.jy_ping_send(jianYanPings);
    }
    private PaiBanKSXXDto zhuanHuaHYXX(JZ_GH_JinRiHYListDto banXX) {
        PaiBanKSXXDto paiBanKSXXDto = new PaiBanKSXXDto();
        paiBanKSXXDto.setYiShengXM(banXX.getYiShengXM());
        paiBanKSXXDto.setRiqi(DateUtil.formatDate(banXX.getRiQi()));
        String shengYuHaoYuan;
        if (banXX.getZuiGaoXH() != null && banXX.getZuiGaoXH().equals(9999)) {
            //如果是不限号的情况下 默认300个号源
            shengYuHaoYuan = "剩余" + (300 - banXX.getYiGuaHSL()) + "号";
        } else if (banXX.getXianHao() !=null && banXX.getXianHao() - banXX.getYiGuaHSL() > 0) {
            shengYuHaoYuan = "剩余" + (banXX.getXianHao() - banXX.getYiGuaHSL()) + "号";
        } else if (banXX.getZuiGaoXH() != null  && banXX.getZuiGaoXH().equals(0) && banXX.getXianHao().equals(0)) {
            //停诊判断标志 最高序号为0  并且 限号为 0
            shengYuHaoYuan = "停诊";
        } else {
            shengYuHaoYuan = "剩余0号";
        }
        paiBanKSXXDto.setShengYuHaoYuan(shengYuHaoYuan);
        paiBanKSXXDto.setKeShiMC(banXX.getKeShiMC());
        paiBanKSXXDto.setGuaHaoLB(banXX.getGuaHaoLBMC());
        paiBanKSXXDto.setShangXiaWu(banXX.getShiDuanMC());
        return paiBanKSXXDto;
    }
    @PostMapping("sendYaoPinChuangKouXX")
    public Boolean sendYaoPinChuangKouXX(List<YaoPinLEDXXDto> chuangKouXXData) throws RuntimeException, ParseException {
        YaoFangXXDto yaoFangXXDto = new YaoFangXXDto();
        /**
         * 此程序设计中窗口号 均为固定值
         */
        final String chuangkouxx_00 = "2463045586518020096";         //中药房
        final String chuangkouxx_01 = "1055522380597723136";         //西药房01
        final String chuangkouxx_02 = "1069590644789379072";         //西药房02
        final String chuangkouxx_03 = "1104398042796163072";         //西药房03
        final String chuangkouxx_04 = "1073214111124324352";         //西药房04
        final String chuangkouxx_05 = "2447705427475959808";         //西药房05

        Map<String, List<YaoFangChuangKouXXDto>> chuangKouXX = chuangKouXXData.stream()
                .filter(x -> StrUtil.isNotBlank(x.getChuangKouID()))
                .map(x -> new YaoFangChuangKouXXDto(x.getChuangKouID(), x.getXingMing()))
                .collect(Collectors.groupingBy(YaoFangChuangKouXXDto::getChuangKouId));
        yaoFangXXDto.setChuangkouxx_00(chuangKouXX.get(chuangkouxx_00));
        yaoFangXXDto.setChuangkouxx_01(chuangKouXX.get(chuangkouxx_01));
        yaoFangXXDto.setChuangkouxx_02(chuangKouXX.get(chuangkouxx_02));
        yaoFangXXDto.setChuangkouxx_03(chuangKouXX.get(chuangkouxx_03));
        yaoFangXXDto.setChuangkouxx_04(chuangKouXX.get(chuangkouxx_04));
        yaoFangXXDto.setChuangkouxx_05(chuangKouXX.get(chuangkouxx_05));

        /**
         * 药房两个屏幕的数据发送
         */
        yaoFangLEDBuilder.wf_send_dp1(yaoFangXXDto);
        yaoFangLEDBuilder.wf_send_dp2(yaoFangXXDto);
        return true;
    }
}
